<?php

namespace view;

class PageView {

	/**
	 * String HTML Shows if the user is logged in or not
	 */
	public $loggedInStatus;
	/**
	 * String HTML Shows if the user is logged in or not
	 */
	public $loggedIn = "<b>Admin är inloggad.</b>";
	/**
	 * String HTML Shows if the user is logged in or not
	 */
	public $notLoggedIn = "<b>Ej inloggad.</b>";

	/**
	 * String contains the username that was used in the latest login attempt
	 */
	public $lastUsername = "";
	
	private $cookieOrPostUsername = "username";
	private $cookieOrPostPassword = "password";
	private $postkeepLoggedIn = "keepLoggedIn";
	
	/**
	 * String contains the appropriate login message for the user
	 */
	private $message;

	//private $notLoggedIn = "Ej inloggad";
	//private $loggedIn = "Inloggad";

	/**
	 * String HTML
	 */
	private $inputField;

	/**
	 * String HTML
	 */
	private $logoutButton = "<form name='logout' id='logout' method='POST'>
							<input type='submit' value='Logga ut'</input></form>";

	/**
	 * @return boolean Returns true if the last pageload was a post
	 */
	public function wasPost(){
		if($_SERVER['REQUEST_METHOD'] === 'POST'){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	/**
	 *@return String return the $_POST content
	 */
	public function getPostUsername() {
		if (isset($_POST[$this -> cookieOrPostUsername])) {
			return $_POST[$this -> cookieOrPostUsername];
		} else {
			return "";
		}
	}

	/**
	 *@return String return the $_POST content
	 */
	public function getPostPassword() {
		if (isset($_POST[$this -> cookieOrPostPassword])) {
			return $_POST[$this -> cookieOrPostPassword];
		} else {
			return "";
		}
	}

	/**
	 *@return Boolean returns true if the keep logged in box is checked
	 */
	public function keepLoggedInCheckbox() {
		if (isset($_POST[$this -> postkeepLoggedIn])) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	// /**
	 // * @return boolean returns true if the user logged out
	 // */
	// public function didUserLogOut() {
		// if(isset($_SESSION['loggedIn'])){
			// if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST' && $_SESSION['loggedIn'] == 'true') {
			// return TRUE;			
			// } else {
				// return FALSE;
			// }
		// } else {
			// return FALSE;
		// }
	// }

	/**
	 * Returns true if there are valid cookies available
	 * @param $username string valid username
	 * @param $password string valid password
	 */
	public function setCookie($username, $hashedPassword){
		$endTime = time () + 60;
		setcookie($this -> cookieOrPostUsername, $username, $endTime);
		setcookie($this -> cookieOrPostPassword, $hashedPassword, $endTime);		
	}

	public function removeCookies(){
		if(isset($_COOKIE[$this -> cookieOrPostUsername])){
			setcookie($this -> cookieOrPostUsername, "", time()-9999);	
		}
		if(isset($_COOKIE[$this -> cookieOrPostPassword])){
			setcookie($this -> cookieOrPostPassword, "", time()-9999);	
		}		
	}
	
	// /**
	 // * @param $username string valid username
	 // * @param $password string valid password
	 // * @return Boolean Returns true if there are cookies 
	 // *		   with the right username and password available
	 // */
	// public function checkCookies($username, $password){
		// if(isset($_COOKIE[$this -> cookieOrPostUsername]) && isset($_COOKIE[$this -> cookieOrPostPassword])){
			// if($_COOKIE[$this -> cookieOrPostUsername] == $username && 
			// $_COOKIE[$this -> cookieOrPostPassword] == $password){
					// return TRUE;
			// }
		// } else {
			// return FALSE;
		// }
	// }
	
	// /**
	 // * @param $username string valid username
	 // * @param $password string valid password 
	 // * @return Boolean returns true if the cookies are valid
	 // */
	// public function validateCookies($username, $password){
		// $cookieEndtime = file_get_contents("cookieExpiry.txt");
		// if($cookieEndtime > time()){
			// return TRUE;
		// }
		// else {
			// return FALSE;
		// }	
	// }
	
	/**
	 * @param $username string valid username
	 * @param $password string valid password
	 * @param $cookieEndtime
	 * @return Boolean returns true if the cookies are valid
	 */
	public function validateAllCookies($username, $password, $cookieEndtime){
		if(isset($_COOKIE[$this -> cookieOrPostUsername]) && isset($_COOKIE[$this -> cookieOrPostPassword])){
			if($_COOKIE[$this -> cookieOrPostUsername] == $username && 
			$_COOKIE[$this -> cookieOrPostPassword] == $password){				
				if($cookieEndtime > time()){
					return TRUE;	
				}
				else {
					return FALSE;
				}
			}
		} else {
			return FALSE;
		}
	}
	
	/**
	 *@return Boolean returns tru if any user info cookies are set
	 */
	public function areSomeCookiesSet(){
		if(isset($_COOKIE[$this -> cookieOrPostUsername]) && isset($_COOKIE[$this -> cookieOrPostPassword])){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	/**
	 * @param $isLoggedIn boolean decides whether to display the login form or not
	 *  Renders the page
	 */
	public function renderHTML($isLoggedIn) {
		$content;
		if (!$isLoggedIn) {
			$content = $this -> getForm();
		} else {
			$content = $this -> logoutButton;
		}

		echo "<!DOCTYPE HTML>
				<html>
					<head>
						<title>Laboration.</title>
						<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
					</head>				
					<body> 
						<h1> Laboration ep222fj </h1>
						<p>" . $this -> loggedInStatus . "
						<br>" . $this -> message . "
						<br> " . $content . "
						<br> " . $this -> setClock() . "
						</p>
					</body>
				</html>";
	}

	/**
	 * Sets the login message
	 * @param $messageId int representing the login message
	 */
	public function setMessage($messageId){
		switch($messageId){
			case 0:
				{
					$this -> message = "";
					break;
				}
			case 1:
				{
					$this -> message = "Inloggning lyckades.";
					break;
				}
			case 2:
				{
					$this -> message = "Inloggning lyckades med hjälp av cookies.";
					break;
				}
			case 3:
				{
					$this -> message = "Inloggning lyckades och vi kommer ihåg dig till nästa gång.";
					break;
				}
			case 4:
				{
					$this -> message = "Du är nu utloggad.";
					break;
				}
			case 5:
				{
					$this -> message = "Användarnamn saknas.";
					break;
				}
			case 6:
				{
					$this -> message = "Lösenord saknas.";
					break;
				}
			case 7:
				{
					$this -> message = "Felaktigt användarnamn och/eller lösenord.";
					break;
				}
			case 8:
				{
					$this -> message = "Felaktig information i cookie.";
					break;
				}
		}
	}

	/**
	 * @return String HTML form
	 */
	private function getForm() {
		$form = "<form name='login' id='login' method='POST'>
					Användarnamn: 
					<input type='text' name='username' id='username' value='" . $this -> lastUsername . "' class='text' maxlenght='20' />
					</br>
					Lösenord: 
					<input type='password' name='password' id='password' class='text' maxlenght='20' />
					</br>
					Håll mig inloggad: 
					<input type='checkbox' name='keepLoggedIn'>
					</br>
					<input type='submit' value='Logga in' />
					</form>";

		return $form;
	}

	/**
	 * @return String with current time and date
	 */
	private function setClock() {
		setlocale(LC_TIME, 'sv_SE', 'sv_SE.UTF-8', 'Swedish_Sweden.1252', 'WINDOWS-1252');		
		return strftime("%A, den %d %B år %Y. Klockan är [%H:%M:%S]");
	}

}
